package org.edu.model;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Proxy;

@Entity
@Proxy(lazy = false)
@Table(name = "USER", catalog = "SCHOOL")
public class User implements Serializable {

	private static final long serialVersionUID = 1L;
	private Long user_id;
	private String user_number;
	private Roles roles;
	private RolesSciency rolesSciency;
	private Sciency sciency;
	private Address address;
	private Student student;
	private Teacher teacher;
	private Admin admin;
	private Set<Document> documents = new HashSet<>();
	private Set<Mail> mails = new HashSet<>();
	private String password;
	private String question;
	private String answer;
	private String sex;
	private String email_r;
	private Boolean status;
	private Byte[] record_ip;
	private Byte[] update_ip;
	private Date record_date;
	private Date update_date;
	private Date last_login_date;

	@Id
	@GeneratedValue
	@Column(name = "USER_ID")
	public Long getUser_id() {
		return user_id;
	}

	public void setUser_id(Long user_id) {
		this.user_id = user_id;
	}

	@Column(name = "USER_NUMBER", length = 40)
	public String getUser_number() {
		return user_number;
	}

	public void setUser_number(String user_number) {
		this.user_number = user_number;
	}

	@ManyToOne
	@JoinColumn(name = "ROLES_ID")
	public Roles getRoles() {
		return roles;
	}

	public void setRoles(Roles roles) {
		this.roles = roles;
	}
	
	@ManyToOne
	@JoinColumn(name = "ROLES_SCIENCY_ID")
	public RolesSciency getRolesSciency() {
		return rolesSciency;
	}

	public void setRolesSciency(RolesSciency rolesSciency) {
		this.rolesSciency = rolesSciency;
	}
	
	@ManyToOne
	@JoinColumn(name = "SCIENCY_ID")
	public Sciency getSciency() {
		return sciency;
	}

	public void setSciency(Sciency sciency) {
		this.sciency = sciency;
	}

	@OneToOne(mappedBy="userAdress", cascade=CascadeType.ALL)
	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}
	
	@OneToOne(mappedBy="userStudent", cascade=CascadeType.ALL)
	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	@OneToOne(mappedBy="userTeacher", cascade=CascadeType.ALL)
	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	@OneToOne(mappedBy="userAdmin", cascade=CascadeType.ALL)
	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	@ManyToMany
	@JoinTable(name = "JOIN_USER_DOCUMENTS", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "DOCUMENTS_ID") })
	public Set<Document> getDocuments() {
		return documents;
	}

	public void setDocuments(Set<Document> documents) {
		this.documents = documents;
	}

	@ManyToMany
	@JoinTable(name = "JOIN_USER_MAIL", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "MAIL_ID") })
	public Set<Mail> getMails() {
		return mails;
	}

	public void setMails(Set<Mail> mails) {
		this.mails = mails;
	}

	@Column(name = "PASSWORD", length = 40)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "SEX", length = 10)
	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Column(name = "EMAIL_R", length = 30)
	public String getEmail_r() {
		return email_r;
	}

	public void setEmail_r(String email_r) {
		this.email_r = email_r;
	}

	@Column(name = "STATUS")
	public Boolean getStatus() {
		return status;
	}

	public void setStatus(Boolean status) {
		this.status = status;
	}

	@Column(name = "QUESTION", length = 50)
	public String getQuestion() {
		return question;
	}

	public void setQuestion(String question) {
		this.question = question;
	}

	@Column(name = "ANSWER", length = 50)
	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	@Column(name = "RECORD_IP")
	public Byte[] getRecord_ip() {
		return record_ip;
	}

	public void setRecord_ip(Byte[] record_ip) {
		this.record_ip = record_ip;
	}

	@Column(name = "UPDATE_IP")
	public Byte[] getUpdate_ip() {
		return update_ip;
	}

	public void setUpdate_ip(Byte[] update_ip) {
		this.update_ip = update_ip;
	}

	@Column(name = "RECORD_DATE")
	public Date getRecord_date() {
		return record_date;
	}

	public void setRecord_date(Date record_date) {
		this.record_date = record_date;
	}

	@Column(name = "UPDATE_DATE")
	public Date getUpdate_date() {
		return update_date;
	}

	public void setUpdate_date(Date update_date) {
		this.update_date = update_date;
	}

	@Column(name = "LAST_LOGIN_DATE")
	public Date getLast_login_date() {
		return last_login_date;
	}

	public void setLast_login_date(Date last_login_date) {
		this.last_login_date = last_login_date;
	}
	
	public User() {
		super();
	}

	public User(Long user_id) {
		super();
		this.user_id = user_id;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((answer == null) ? 0 : answer.hashCode());
		result = prime * result + ((email_r == null) ? 0 : email_r.hashCode());
		result = prime * result
				+ ((last_login_date == null) ? 0 : last_login_date.hashCode());
		result = prime * result
				+ ((password == null) ? 0 : password.hashCode());
		result = prime * result
				+ ((question == null) ? 0 : question.hashCode());
		result = prime * result
				+ ((record_date == null) ? 0 : record_date.hashCode());
		result = prime * result + Arrays.hashCode(record_ip);
		result = prime * result + ((sex == null) ? 0 : sex.hashCode());
		result = prime * result + ((status == null) ? 0 : status.hashCode());
		result = prime * result
				+ ((update_date == null) ? 0 : update_date.hashCode());
		result = prime * result + Arrays.hashCode(update_ip);
		result = prime * result + ((user_id == null) ? 0 : user_id.hashCode());
		result = prime * result
				+ ((user_number == null) ? 0 : user_number.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (answer == null) {
			if (other.answer != null)
				return false;
		} else if (!answer.equals(other.answer))
			return false;
		if (email_r == null) {
			if (other.email_r != null)
				return false;
		} else if (!email_r.equals(other.email_r))
			return false;
		if (last_login_date == null) {
			if (other.last_login_date != null)
				return false;
		} else if (!last_login_date.equals(other.last_login_date))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		if (question == null) {
			if (other.question != null)
				return false;
		} else if (!question.equals(other.question))
			return false;
		if (record_date == null) {
			if (other.record_date != null)
				return false;
		} else if (!record_date.equals(other.record_date))
			return false;
		if (!Arrays.equals(record_ip, other.record_ip))
			return false;
		if (sex == null) {
			if (other.sex != null)
				return false;
		} else if (!sex.equals(other.sex))
			return false;
		if (status == null) {
			if (other.status != null)
				return false;
		} else if (!status.equals(other.status))
			return false;
		if (update_date == null) {
			if (other.update_date != null)
				return false;
		} else if (!update_date.equals(other.update_date))
			return false;
		if (!Arrays.equals(update_ip, other.update_ip))
			return false;
		if (user_id == null) {
			if (other.user_id != null)
				return false;
		} else if (!user_id.equals(other.user_id))
			return false;
		if (user_number == null) {
			if (other.user_number != null)
				return false;
		} else if (!user_number.equals(other.user_number))
			return false;
		return true;
	}

}
